package com.ycw.controller.WJK;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycw.bean.Admin;
import com.ycw.bean.Projects;
import com.ycw.bean.Users;
import com.ycw.service.WJK.WeiService;
import com.ycw.util.Msg;

@Controller
public class WeiController {

	@Autowired
	WeiService weiService;
	
	
	//判断用户邮箱
	@RequestMapping("/checkUsers")
	@ResponseBody
	public Msg checkUsers(String usEmail) {
		//System.out.println("usEmail=  "+usEmail);
		boolean f=weiService.checkUsers(usEmail);
		if (f) {
			return Msg.success();
		}else {
			return Msg.fail();
		}
	}
	
	//用户登录
	@RequestMapping("/usersLogin")
	public String usersLogin(Users users,HttpSession session) {
		//System.out.println(users.getUsEmail()+users.getUsPassword());
		users=weiService.usersLogin(users);
		
		if (users==null) {
			return "/login.jsp";
		}else {
			session.setAttribute("users", users);
			System.out.println(users.getUsId()+"  "+users.getUsRole());
			return "redirect:index";
		}
		
	}
	
	//管理员登录
	@RequestMapping("/adminLogin")
	public String adminLogin(Admin admin,HttpSession session) {
		System.out.println(admin.getAdminName()+admin.getAdminPassword());
		admin=weiService.adminLogin(admin);
		if (admin==null) {
			return "/login1.jsp";
		}else {
			session.setAttribute("admin", admin);
			System.out.println(admin.getAdminId());
			return "redirect:index1";
		}
			
	}
	
	
	//查看所有项目
	@RequestMapping("/getAllProjects")
	@ResponseBody
	public Msg getAllProjects(@RequestParam(value="pn",defaultValue="1")Integer pn) {
		PageHelper.startPage(pn,5);
		List<Projects> list=weiService.getAllProjects();
		PageInfo page=new PageInfo(list);
		
		return Msg.success().add("pageInfo",page);
	}
	
	
	//详情页
	@RequestMapping("/getProjectDetaile")
	@ResponseBody
	public Msg getProjectDetaile(Integer psId,@RequestParam(value="pn",defaultValue="1")Integer pn) {
		System.out.println("psId=  "+psId);
		Map<String, Object> map=weiService.getProjectDetaile(psId,pn);
		return Msg.success().add("map", map);
	}
}
